extends "../../../azurlane_ctrl/ChaInfo.gd"
func _info():
	pass
var inited = false
func _connect():
	._connect()
func _extInit():
	._extInit()
	chaName = "小卖部"
	attInfox.maxHp = 1
	lv = 1
	aiOn = false
	isDrag = false
	addSkillTxt("此单位不可参与战斗，为功能性NPC")
	addSkill("每回合开始时，会自动将身上的第一件装备随机替换成另一件装备，并强制扣除8金币", "折旧换新")
	addSkill("放入3架普通舰载机，获得一架随机稀有舰载机", "舰载机合成")
	# addSkill("点击装备出售按钮，卖出身上的所有装备", "装备出售")
	type = sys.CharaClass.NPC
	mainFlag = false
	torpFlag = false
	if sys.main != null and not sys.main.is_connected("onBattleStart", self, "onBattleStart"):
		sys.main.connect("onBattleStart",self,"onBattleStart")

func onBattleStart():
	if sys.main.player.gold < 8:
		return
	if items.empty():return
	var it = items[0]
	if it.id == "i_ex_equip_plane" or it.price == 0:
		return
	sys.main.player.addItem(sys.newItem(itemData.rndGet(true)))
	sys.main.player.subGold(8)
	delItem(it)
	sys.main.player.delItem(it)

func addItem(item):
	if not .addItem(item):return
	var masCha = item.masCha
	if item.id == "i_ex_equip_agreedProof":
		agreedProof()
		return
	elif item.id == "i_ex_equip_huamingce":
		huamingce()
		return
	elif item.id == "i_rare_equip_directBox":
		directBox()
		return
	elif item.id == "i_rare_equip_equipBox":
		equipBox()
		return
	elif item.id == "i_rare_equip_limitProof":
		limitProof()
		return
	if item.id == "i_ex_equip_plane":
		var n = 0
		for i in masCha.items:
			if  i.id == item.id:
				n += 1
		if n == 3:
			yield(sys.get_tree().create_timer(0.1), "timeout")
			if item.id == "i_ex_equip_plane":
				var it = sys.newItem("i_ex_equip_plane")
				it.postInit(true)
				sys.main.player.addItem(it)
			var index = masCha.items.size() - 1
			while(index >= 0):
				var i = masCha.items[index]
				sys.main.player.delItem(i)
				index -= 1

func limitProof():
	if azurCtrl.upCha != null:
		sys.main.player.addCha(sys.main.newChara(azurCtrl.upCha.id))
		for i in items:
			if i.id == "i_rare_equip_limitProof":
				i.delSelf()

func chaInfo(cha):
	var charaInfos = []
	for i in sys.get_node("/root/topUi").get_children():
		if i.name.find("charaInfoMsg") > -1:charaInfos.append(i)
	var charaInfo = charaInfos.pop_back()

	# var btnBoxR = utils.createHBox(charaInfo, Vector2(900, 600))
	var btnBoxL = utils.createHBox(charaInfo, Vector2(100, 600))
	
	# utils.createItemButton(btnBoxR, self, "战斗详情", "battleDetail", [cha, charaInfo])
	# utils.createItemButton(btnBoxR, self, "状态统计", "buffDetail", [cha, charaInfo])
	# utils.createItemButton(btnBoxL, self, "切换立绘", "switchDrawing", [cha, charaInfo])

	# utils.createItemButton(btnBoxR, self, "校准射程", "changeAtkRan", [charaInfo])

	# if cha.team == 1:
	utils.createItemButton(btnBoxL, self, "脱下装备", "removeAllEquip", [])
	charaInfo.get_node("Button").hide()

#点击角色时调用此函数获得角色信息文本
func getInfo():
	var res = "\n"

	# for i in skillStrs:
	# 	var tmp = i.rsplit("{tmpName}")
	# 	i = tmp[0]
	# 	if tmp.size() == 2:
	# 		var skName = tmp[1]
	# 		var skData = skillData.get(skName)
	# 		if skData != null:
	# 			var cd = skData.get("cd")
	# 			var id = skData.get("id")
	# 			var sk
	# 			if id != null:
	# 				sk = getSkill(id)
	# 			if sk != null:
	# 				i = i.replace("{cd}", utils.floatScale(sk.cd/(1+att.cd)))
	# 			var dmg = self.getSkillEffect(skName)
	# 			if dmg != null:
	# 				i = i.replace("{damage}", int(dmg))
	# 	res += i + "\n"
	# res = appendMeta(res)
	return "[color=%s]%s[/color]" % [config.itemGradeColors[lv - 1], chaName] + res

#出售装备
func sellItems(cha):
	var index = cha.items.size()-1
	while index >= 0:
		var item = cha.items[index]
		sys.main.player.plusGold(item.price)
		sys.main.player.delItem(item)
		index -= 1
	
######################################约定的证明##############################
#约定的证明
func agreedProof():
	var specChaWindow = utils.createWindowDialog(sys.main.get_node("ui"), "特殊池角色", Vector2(620, 800))
	var context = utils.createScrollContainer(specChaWindow, Vector2(550, 720), Vector2(30, 50))
	specChaWindow.popup_exclusive = true
	specChaWindow.get_close_button().hide()

	var taskBox = utils.createGridContainer(context, 3)
	taskBox.rect_position = Vector2(50,50)
	taskBox.set_custom_minimum_size(Vector2(550, 750))
	var chas = utils.arrayCopy(chaData.upChas)
	for i in chaData.rndPoolRsl:
		chas.append(chaData.infoDs[i])

	for i in chas:
		utils.createRichTextLabel(utils.consts.colorRed + i.name, taskBox, Vector2(400, 50))
		utils.createItemButton(taskBox, self, "选择", "doGetSpecCha", [i, specChaWindow])
		utils.createItemButton(taskBox, self, "查看", "viewSpecCha", [i])
	specChaWindow.popup_centered()
var bossWindow
var chaContainer
func viewSpecCha(conf):
	if not is_instance_valid(bossWindow):
		bossWindow = utils.createWindowDialog(sys.main.get_node("ui"), "目标信息", Vector2(600, 400))
		chaContainer = CenterContainer.new()
		chaContainer.set_custom_minimum_size(Vector2(550, 380))
		bossWindow.add_child(chaContainer)
	for i in chaContainer.get_children():
		i.queue_free()
	var chaBt = preload("res://ui/itemBt/itemBt.tscn").instance()
	chaBt.id = chaData.getLvIds(conf.id, 4)
	chaContainer.add_child(chaBt)
	bossWindow.popup_centered()

func doGetSpecCha(conf, specChaWindow):
	specChaWindow.hide()
	specChaWindow.queue_free()
	sys.main.player.addCha(sys.main.newChara(conf.id))
	for i in items:
		if i.id == "i_ex_equip_agreedProof":
			sys.main.player.delItem(i)

######################################花名册##############################
#约定的证明
func huamingce():
	var specChaWindow = utils.createWindowDialog(sys.main.get_node("ui"), "选择角色", Vector2(620, 800))
	var context = utils.createScrollContainer(specChaWindow, Vector2(550, 720), Vector2(30, 50))
	specChaWindow.popup_exclusive = true
	specChaWindow.get_close_button().hide()

	var taskBox = utils.createGridContainer(context, 3)
	taskBox.rect_position = Vector2(50,50)
	taskBox.set_custom_minimum_size(Vector2(550, 750))

	for i in chaData.rndPoolRsl:
		utils.createRichTextLabel(utils.consts.colorRed + chaData.infoDs[i].name, taskBox, Vector2(400, 50))
		utils.createItemButton(taskBox, self, "选择", "doGetNorCha", [i, specChaWindow])
		utils.createItemButton(taskBox, self, "查看", "viewNorCha", [i])
	specChaWindow.popup_centered()
func viewNorCha(conf):
	if not is_instance_valid(bossWindow):
		bossWindow = utils.createWindowDialog(sys.main.get_node("ui"), "目标信息", Vector2(600, 400))
		chaContainer = CenterContainer.new()
		chaContainer.set_custom_minimum_size(Vector2(550, 380))
		bossWindow.add_child(chaContainer)
	for i in chaContainer.get_children():
		i.queue_free()
	var chaBt = preload("res://ui/itemBt/itemBt.tscn").instance()
	chaBt.id = chaData.getLvIds(conf, 4)
	chaContainer.add_child(chaBt)
	bossWindow.popup_centered()

func doGetNorCha(conf, specChaWindow):
	specChaWindow.hide()
	specChaWindow.queue_free()
	sys.main.player.addCha(sys.main.newChara(conf))
	for i in items:
		if i.id == "i_ex_equip_huamingce":
			sys.main.player.delItem(i)

######################################定向补给箱##############################
func directBox():
	var itemWindow = utils.createWindowDialog(sys.main.get_node("ui"), "选择装备", Vector2(900, 700))
	itemWindow.popup_exclusive = true
	itemWindow.get_close_button().hide()

	var context = utils.createScrollContainer(itemWindow, Vector2(820, 620), Vector2(30, 50))

	var itemGrid = utils.createGridContainer(context, 8)
	itemGrid.rect_position = Vector2(20, 20)
	itemGrid.set_custom_minimum_size(Vector2(800,0))
	for i in utils.consts.itemsRare:
		var itemPanel = Panel.new()
		itemPanel.name = "panel"
		itemPanel.rect_min_size = Vector2(100, 120)
		var obj = preload("res://ui/item/item.tscn").instance()
		var items = sys.newItem(i)
		obj.init(items)
		obj.isDrag = false
		obj.rect_position = Vector2(30, 25)
		itemPanel.add_child(obj)
		utils.createItemButton(itemPanel, self, "选择", "doGetSpecItem", [i, itemWindow], Vector2(20, 80))
		itemGrid.add_child(itemPanel)
	itemWindow.popup_centered()
func doGetSpecItem(conf, itemWindow):
	itemWindow.hide()
	itemWindow.queue_free()
	sys.main.player.addItem(sys.newItem(conf))
	for i in items:
		if i.id == "i_rare_equip_directBox":
			sys.main.player.delItem(i)

######################################补给箱##############################
func equipBox():
	var itemWindow2 = utils.createWindowDialog(sys.main.get_node("ui"), "选择装备", Vector2(900, 700))
	itemWindow2.popup_exclusive = true
	itemWindow2.get_close_button().hide()

	var context = utils.createScrollContainer(itemWindow2, Vector2(820, 620), Vector2(30, 50))

	var itemGrid = utils.createGridContainer(context, 8)
	itemGrid.rect_position = Vector2(20, 20)
	itemGrid.set_custom_minimum_size(Vector2(800,0))

	for i in itemData.rndPool.items:
		var itemId = i[0].id
		var itemPanel = Panel.new()
		itemPanel.name = "panel"
		itemPanel.rect_min_size = Vector2(100, 120)
		var obj = preload("res://ui/item/item.tscn").instance()
		var items = sys.newItem(itemId)
		obj.init(items)
		obj.isDrag = false
		obj.rect_position = Vector2(30, 25)
		itemPanel.add_child(obj)
		utils.createItemButton(itemPanel, self, "选择", "doGetSpecItem2", [itemId, itemWindow2], Vector2(20, 80))
		itemGrid.add_child(itemPanel)
	itemWindow2.popup_centered()

func doGetSpecItem2(conf, itemWindow2):
	itemWindow2.hide()
	itemWindow2.queue_free()
	sys.main.player.addItem(sys.newItem(conf))
	for i in items:
		if i.id == "i_rare_equip_equipBox":
			sys.main.player.delItem(i)

